package client.dialog;

import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;

import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class KickMemberDialog {

    public static void show(List<String> groupMembers, Consumer<List<String>> onConfirm) {
        Dialog<List<String>> dialog = new Dialog<>();
        dialog.setTitle("踢人出群");
        dialog.setHeaderText("选择要踢出的成员");

        dialog.getDialogPane().getStylesheets().add(
                KickMemberDialog.class.getResource("/css/dialog.css").toExternalForm()
        );
        dialog.getDialogPane().getStyleClass().addAll("custom-dialog", "red");


        ButtonType confirmButtonType = new ButtonType("踢出", ButtonBar.ButtonData.OK_DONE);
        dialog.getDialogPane().getButtonTypes().addAll(confirmButtonType, ButtonType.CANCEL);

        VBox vbox = new VBox(10);
        vbox.setPadding(new Insets(10));
        dialog.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);

        // 创建复选框
        List<CheckBox> checkBoxes = groupMembers.stream()
                .map(member -> {
                    CheckBox cb = new CheckBox(member);
                    cb.getStyleClass().add("custom-checkbox");
                    return cb;
                })
                .collect(Collectors.toList());

        vbox.getChildren().addAll(checkBoxes);

        ScrollPane scrollPane = new ScrollPane(vbox);
        scrollPane.setFitToWidth(true);
        scrollPane.getStyleClass().add("custom-scroll");

        dialog.getDialogPane().setContent(scrollPane);

        dialog.setResultConverter(dialogButton -> {
            if (dialogButton == confirmButtonType) {
                return checkBoxes.stream()
                        .filter(CheckBox::isSelected)
                        .map(CheckBox::getText)
                        .collect(Collectors.toList());
            }
            return null;
        });

        Optional<List<String>> result = dialog.showAndWait();
        result.ifPresent(onConfirm);
    }
}
